package org.jeecg.modules.flower.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags="移动端接口")
@RestController
@RequestMapping("/flower/app")
@Slf4j
public class FlowerAppController {

    @Autowired
    private ISysDictService sysDictService;
    @AutoLog
    @ApiOperation(value = "app升级查询",notes = "app升级查询")
    @GetMapping(value = "/update")
    public Result<?> appUpdate(@RequestParam(name = "appid",defaultValue = "caihou001") String appid, @RequestParam(name = "version") String version,
                               @RequestParam(name = "imei",defaultValue = "imei") String imei){
        String dbAppid = "";
        String dbVersion= "";
        String downloadUrl = "";
        String contexts = "";
        dbAppid  = sysDictService.queryDictTextByKey("planting_app", "1");
        dbVersion = sysDictService.queryDictTextByKey("planting_app", "2");
        downloadUrl = sysDictService.queryDictTextByKey("planting_app", "3");
        contexts = sysDictService.queryDictTextByKey("planting_app", "4");
//        Result<String> app1 = sysDictServiceClient.getDictTextOpen("planting_app", "1");
//        Result<String> app2 = sysDictServiceClient.getDictTextOpen("planting_app", "2");
//        Result<String> app3 = sysDictServiceClient.getDictTextOpen("planting_app", "3");
//        if (app1 != null) {
//            dbAppid = app1.getResult();
//        }
//        if (app2 != null) {
//            dbVersion = app2.getResult();
//        }
//        if (app3 != null) {
//            downloadUrl = app3.getResult();
//        }
        Result<JSONObject>  res = new Result<JSONObject>();
        JSONObject jsonObject = new JSONObject();
//        if (dbAppid.equals(appid)) {
        if (!dbVersion.equals(version)) {
            jsonObject.put("version",dbVersion);
            jsonObject.put("downloadUrl",downloadUrl);
            jsonObject.put("contexts",contexts);
            res.setResult(jsonObject);
            return  res;
        } else {
            jsonObject.put("version", null);
            res.setResult(jsonObject);
            return res;
        }
//        } else {
//            return Result.error("appid不匹配");
//        }

    }
}
